#include "addmenu.h"
#include "ui_addmenu.h"
#include <QPixmap>
#include <QFile>
#include<iostream>
#include<fstream>
#include<QString>

addmenu::addmenu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::addmenu)
{
    ui->setupUi(this);
    file = new QFileDialog(this);
    haveimg =true;
}

addmenu::~addmenu()
{
    delete ui;
    delete file;
}

void addmenu::init(Getinftonet *gifs){
reqpost = gifs;
}

void addmenu::on_pushButton_clicked()
{
    if(haveimg == false) return; //已存在图片

    filepath = "";
    filepath = file->getOpenFileName(this, tr("Open File"),
                                     "/home",
                                     tr("Images (*.png *.xpm *.jpg)"));
    if(filepath == "") return;
    qDebug()<<filepath;
    ui->pushButton->setStyleSheet(QString("border-image:url(%1)").arg(filepath));
    haveimg = false;
}

void addmenu::on_pushButton_2_clicked()
{
    haveimg = true;
    emit ui->pushButton->clicked();
}

void addmenu::on_pushButton_3_clicked()
{
    mymenu menu;

    QFile file(filepath);
    file.open(QIODevice::ReadOnly);
    menu.hot = "0";
    menu.sid = shopunit;
    menu.name = ui->lineEdit_name->text();
    menu.minet= ui->lineEdit_time->text();
    menu.money =ui->lineEdit_meny->text();
    menu.photo =file.readAll();
    menu.type = ui->type->text();

if(ui->checkBoxb->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");
if(ui->checkBox_l->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");
if(ui->checkBox_e->isChecked()) menu.eattype.append("1");
else menu.eattype.append("0");


QByteArray buf;
buf.append(QString("{"
           "\"sid\":\"%1\","
           "\"name\":\"%2\","
           "\"hot\":\"%3\","
           "\"minet\":\"%4\","
           "\"eattype\":\"%5\","
            "\"money\":\"%6\","
            "\"type\":\"%7\","
            "\"imgtype\":\"jpg\","
           "\"length\":\"%8\",")
           .arg(menu.sid).arg(menu.name).arg(menu.hot)
           .arg(menu.minet).arg(menu.eattype).arg(menu.money).arg(menu.type)
           .arg(QString::number(menu.photo.length())).toUtf8());
buf.append("\"photo\":\"");
buf+=menu.photo;
buf.append("\"}");
reqpost->topost("http://hunya.fun:9000/cliaddmen",buf);
file.close();
}
